'use strict'
import React, { Component }from 'react';

import {
	View,
	Image,
	TextInput,
	ScrollView,
	StyleSheet,
	Text,
	TouchableOpacity,
} from 'react-native';

import DCAction from '../actions/DCAction';
import {NavBarBackItem, NavBarSignItem, NavBarTitleItem} from './NavbBarItems';
import NavigatorBarView from './NavigatorBarView';
import NotifyActions from '../actions/NotifyActions'
var InputCell = require('./RegisterCell').InputCell;


export default class Content extends Component{

	constructor(props){
        super();
        this.state={
        	username: undefined,
        	name: undefined,
        	password: undefined,
        	comfirmPassword: undefined,
        	userData: undefined,
        	models0: undefined,
        };
    };
	updateUserData =()=> {
		
		//请输入用户名
		if (this.state.username == undefined || this.state.username.length == 0) {			
			ALT.actions.DCAction.showText('请输入手机号', 1)
		}else if (this.state.name == undefined || this.state.name.length == 0) {
			ALT.actions.DCAction.showText('请输入真实姓名', 1)
		}else if (this.state.password == undefined || this.state.password.length == 0  
				|| this.state.comfirmPassword == undefined || this.state.comfirmPassword.length == 0) {
			//请输入密码
			ALT.actions.DCAction.showText('请输入密码',1)
		}else if(this.state.password != this.state.comfirmPassword){
			//输入密码不一致
			ALT.actions.DCAction.showText('输入密码与原密码不一致', 1);
		}else{
			//注册请求接口
			var userData = Immutable.fromJS({username: this.state.username,
										 	 password: this.state.password,
										 	 name: this.state.name})
	
			this.state.userData = JSON.stringify(userData)

			if (this.state.userData) {
				ALT.actions.DCAction.regist(this.state.userData)
			}else{
				alert('未知错误')
			}
			
		}
		
	};
	
	popView = ()=> {		
		//退出当前窗口
		nav.pop();
	};
	componentDidMount() { 

        NotifyActions.addNotifier('regist', this.popView, this);
    
    };

    componentWillUnmount() {
        NotifyActions.removeNotifier('regist', this.popView, this);
    };
	render() {

		this.state.models0 = [['用户名','请输入手机号',(a)=>{this.setState({username: a})}],
							 ['真实姓名','请输入真实姓名',(a)=>{this.setState({name: a})}],
			  		 		 ['密码','请输入密码',(a)=>{this.setState({password: a})}],
			   		 		 ['确认密码','请输入确认密码',(a)=>{this.setState({comfirmPassword:a})}]];
		var content;
		content = this.state.models0.map((data,i)=> {
			return <InputCell key={i} 
							  title={data[0]} 
							  placeholder={data[1]} 
							  onChangeText={data[2]}
							  required={true}/>;
		})
		
		return (
			<View style={styles.content}>
				<NavigatorBarView
						leftItem={<NavBarBackItem
						isTabBar={this.props.containerMarginBottom > 0? true: false}/>}
						centerItem ={<NavBarTitleItem
						title='注册'/>}/>

				
				{content}
				<TouchableOpacity style={styles.regist} onPress={()=>{this.updateUserData()}}>
					<Text>
						注册
					</Text>
				</TouchableOpacity>

			</View>
			
		);
	}
};



var styles = StyleSheet.create({
	content: {
		width: Common.size.width,
		height:Common.size.height,
		backgroundColor: '#F2F2F2',
	},
	regist:{
		height:as(35),
		width:Common.size.width-as(20),
		marginTop:as(20),
		marginBottom:as(20),
		justifyContent:'center',
		alignItems:'center',
		alignSelf:'center',
		borderRadius:as(5),
		backgroundColor: '#00bfff'
	},
	valiButton:{
		width:as(80),
		height:as(30),
		marginRight:as(8),
		justifyContent:'center',
		alignItems:'center',	
	},
});





